"""
Reads .bib files in citations/ and combines them into one file.
Also updates the docs/bilbiography.mb file
"""

import os
from pybtex.database.input import bibtex

# Define the directory to walk through
dir_path = '../citations'

# Define the output file
output_file = '../bibliography.bib'

# Define the output markdown file
output_md_file = '../docs/bibliography.md'

# Create a BibTeX parser
parser = bibtex.Parser()

# Keep track of processed .bib files and titles
processed_files = set()
processed_titles = set()

# Open the output file in write mode
with open(output_file, 'w') as outfile:
    outfile.write('******************\n\nAUTOGENERATED: DO NOT MODIFY \n\n******************')
    # Walk through the directory
    for dirpath, dirnames, filenames in os.walk(dir_path):
        # Sort the directories and files
        dirnames.sort()
        filenames.sort()
        # For each file in the directory
        for filename in filenames:
            # If the file is a .bib file and hasn't been processed before
            if filename.endswith('.bib') and filename not in processed_files:
                # Add the file to the set of processed files
                processed_files.add(filename)

                # Construct the full file path
                filepath = os.path.join(dirpath, filename)
                # Open the .bib file in read mode
                with open(filepath, 'r') as infile:
                    # Write the contents of the .bib file to the output file
                    bib_content = infile.read()
                    outfile.write(bib_content)
                    # Write a newline character to the output file
                    outfile.write('\n')

# Read the existing markdown file
with open(output_md_file, 'r') as mdfile:
    lines = mdfile.readlines()

# Find the index of the line after the autogeneration marker
index = next(i for i, line in enumerate(lines) if line.strip() == '<!-- AUTOGENERATED BELOW, DO NOT EDIT -->') + 1

# Write the new markdown content
with open(output_md_file, 'w') as mdfile:
    # Write the lines before the autogeneration marker
    mdfile.writelines(lines[:index])

    # Walk through the directory
    for dirpath, dirnames, filenames in os.walk(dir_path):
        # Sort the directories and files
        dirnames.sort()
        filenames.sort()
        # Write the directory name to the markdown file
        if dirpath != dir_path:
            heading_level = '##' if dirpath.count(os.sep) == dir_path.count(os.sep) + 1 else '###'
            heading_text = os.path.basename(dirpath).replace('_', ' ').title()
            mdfile.write(f'{heading_level} {heading_text}\n\n')
        # For each file in the directory
        for filename in filenames:
            # If the file is a .bib file
            if filename.endswith('.bib'):
                # Construct the full file path
                filepath = os.path.join(dirpath, filename)
                # Parse the .bib file
                bib_database = parser.parse_file(filepath)

                # Write the heading to the markdown file
                heading_level = '##' if dirpath.count(os.sep) == dir_path.count(os.sep) else '###'
                heading_text = filename[:-4].replace('_', ' ').title()
                mdfile.write(f'{heading_level} {heading_text}\n\n')

                # For each entry in the .bib file
                for citation_key in bib_database.entries:
                    entry = bib_database.entries[citation_key]
                    # Extract the title, author, year
                    title = entry.fields.get('lp_title', entry.fields.get('title', 'No title'))
                    if title in processed_titles:
                        continue
                    processed_titles.add(title)
                    if "author" in entry.persons:
                        author_list = entry.persons['author']
                    else: 
                        print(entry.persons)
                        author_list = entry.persons['authors']
                    author = ' and '.join(str(person) for person in author_list)
                    year = entry.fields.get('year', 'No year')
                    # Write the entry to the output file
                    mdfile.write(f'#### {title}(@{citation_key})\n\n')